import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:namewava/utils/jc_cache.dart';
import 'package:namewava/utils/jc_image_path.dart';
import 'package:namewava/utils/jc_nav.dart';
import 'package:namewava/utils/jc_screenutil_info.dart';
import 'package:namewava/utils/jc_text_style.dart';

class JcCalenderBwl extends StatefulWidget {
  const JcCalenderBwl({super.key});

  @override
  State<JcCalenderBwl> createState() => _JcCalenderBwlState();
}

class _JcCalenderBwlState extends State<JcCalenderBwl> {
  TextEditingController _textEditingController =
      TextEditingController(text: '');
  List records = [];
  Map? nowDic;
  String? _bwlName;
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _getMyBwl();
    });
  }

  _getMyBwl() {
    var wavaNameRecord = JcCache.getInstance().get('myBwlRecord');
    var wavaUserId = JcCache.getInstance().get('wavaUserId');
    records = [];
    if (wavaNameRecord == null) {
      records = [];
    } else {
      List t = jsonDecode(wavaNameRecord);
      for (var element in t) {
        if (element['userId'] == wavaUserId) {
          nowDic = element;
          _bwlName = element['bwlName'];
          _textEditingController = TextEditingController(text: _bwlName);
        } else {
          records.add(element);
        }
      }
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GestureDetector(
        child: Container(
          height: JcScreenutilInfo.screenHeight,
          width: JcScreenutilInfo.screenWidth,
          decoration: const BoxDecoration(
              image: DecorationImage(
                  image: AssetImage(JcImagePath.jcMinePageBg),
                  fit: BoxFit.fill)),
          child: Column(
            children: [
              JcNav(
                title: 'synchro',
                rightImage: JcImagePath.jcWavaBwlRightSure,
                backFunction: () {
                  Get.back();
                },
                rightBtnEvent: () {
                  _saveBwl();
                },
              ),
              SizedBox(
                height: 65.h,
              ),
              _bwlNameWidget()
            ],
          ),
        ),
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
      ),
    );
  }

  _bwlNameWidget() {
    return Container(
        margin: EdgeInsets.only(left: 40.w, right: 40.w),
        height: 100.h,
        child: TextField(
          controller: _textEditingController,
          textInputAction: TextInputAction.done,
          maxLines: 5,
          style: JcTextStyle.poetsenOneRegularTextStyle(15.sp, Colors.black),
          decoration: InputDecoration(
              hintText: 'synchro',
              contentPadding: EdgeInsets.zero,
              border: const OutlineInputBorder(borderSide: BorderSide.none),
              hintStyle: JcTextStyle.poetsenOneRegularTextStyle(
                  15.sp, const Color.fromRGBO(181, 181, 181, 1))),
          onChanged: (v) {
            _bwlName = v;
            // _petNameDeal(v);
          },
          onSubmitted: (v) {
            _bwlName = v;
            //  _petNameDeal(v);
          },
        ));
  }

  _saveBwl() {
    if (_bwlName == null || _bwlName == '') {
      EasyLoading.showError('Please input');
      return;
    }
    if (nowDic == null) {
      var wavaUserId = JcCache.getInstance().get('wavaUserId');
      nowDic = {'userId': wavaUserId, 'bwlName': _bwlName};
    } else {
      nowDic!['bwlName'] = _bwlName;
    }
    records.add(nowDic);
    JcCache.getInstance().setData('myBwlRecord', jsonEncode(records));
    Get.back();
  }
}
